<template>
  <div class="login-container">
    <div class="box">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on"
               label-position="left">
        <!-- <span class="tt ul"></span>
        <span class="tt ur"></span>
        <span class="tt dl"></span>
        <span class="tt dr"></span> -->
        <div class="title-container">
          <h3 class="title">用户登录</h3>
        </div>

        <el-form-item prop="username">
          <span class="svg-container">
            <svg-icon icon-class="user"/>
          </span>
          <el-input
            ref="username"
            v-model="loginForm.username"
            placeholder="Username"
            name="username"
            type="text"
            tabindex="1"
            auto-complete="on"
          />
        </el-form-item>

        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password"/>
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="Password"
            name="password"
            tabindex="2"
            auto-complete="on"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"/>
          </span>
        </el-form-item>
        <!-- <div class="fn-clear">
          <el-form-item prop="code" style="width:210px;float:left">
            <span class="svg-container">
              <i class="el-icon-s-promotion"/>
            </span>
            <el-input
              ref="code"
              v-model="loginForm.code"
              placeholder="auth code"
              name="code"
              type="text"
              tabindex="3"
              @keyup.enter.native="handleLogin"
            />
          </el-form-item>
          <div style="float:left;width:calc(100% - 220px);height:49px;margin-left:10px" @click="refreshCode">
            <img style="width:100%;height:100%" :src="'data:image/png;base64,' + codeImg">
          </div>
        </div> -->

        <!-- <el-form-item style="display:none" prop="uuid">
          <el-input
            ref="uuid"
            v-model="loginForm.code"
            placeholder="auth code"
            name="uuid"
            type="text"
            tabindex="3"
            auto-complete="on"
          />
        </el-form-item> -->

        <el-button :loading="loading" type="primary" style="width:100%;" @click.native.prevent="handleLogin">登录
        </el-button>

        <!-- <div class="tips">
          <span style="margin-right:20px;">用户名: admin</span>
          <span> 密码: any</span>
        </div> -->

      </el-form>
    </div>
  </div>
</template>

<script>
import {validUsername} from '@/utils/validate'
import {mapGetters} from 'vuex'
import { SHA256 } from 'crypto-js'
// import {codeImg} from './api'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      // if (!validUsername(value)) {
      //   callback(new Error('Please enter the correct user name'))
      // } else {
      //   callback()
      // }
      if (value.length < 1) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 4) {
        callback(new Error('密码不能小于4位'))
      } else {
        callback()
      }
    }
    const validateCode = (rule, value, callback) => {
      if (value.length < 1) {
        callback(new Error('验证码不能为空'))
      } else {
        callback()
      }
    }

    return {
      loginForm: {
        username: '',
        password: '',
        // code: '',
        // uuid: ''
      },
      loginRules: {
        username: [{required: true, trigger: 'blur', validator: validateUsername}],
        password: [{required: true, trigger: 'blur', validator: validatePassword}],
        code: [{required: true, trigger: 'blur', validator: validateCode}]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      codeImg: ''
    }
  },
  created() {
    // this.getCodeImg()
  },
  computed: {
    ...mapGetters(['userInfo'])
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    // getCodeImg() {
    //   codeImg().then(res => {
    //     if (res.code === '000000') {
    //       this.codeImg = res.data.img
    //       this.loginForm.uuid = res.data.uuid
    //     } else {
    //       this.$message.error('获取验证码出错')
    //     }
    //   })
    // },
    // refreshCode() {
    //   this.getCodeImg()
    // },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          // const formDate = new FormData()
          // for (var key in this.loginForm) {
          //   formDate.append(key, this.loginForm[key])
          // }
          const params = {
            username:this.loginForm.username,
            password: SHA256(this.loginForm.password).toString()
          }
          this.$store.dispatch('user/login', params).then((res) => {
            if (res.code === 200) {
              this.$router.push({path: this.redirect || '/'})
            } else {
              this.$message.error(res.data)
            }
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  background: url('../../assets/bg_3.jpeg') center center no-repeat, linear-gradient(-135deg, #01c6fa 0%, #2b3fba 100%) !important;
  width: 100%;
  height: 100%;
  background-size: cover;
  position: relative;

  .el-icon-s-promotion {
    font-size: 18px;
  }

  .box {
    width: 1065px;
    height: 530px;
    position: absolute;
    left: 50%;
    margin-left: -538px;
    top: 50%;
    margin-top: -300px;
  }

  .el-button {
    padding: 15px 20px;
    margin-top: 10px;
  }

  .el-input {
    display: inline-block;
    height: 48px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #2b2b2b;
      height: 47px;
      caret-color: #534e4e;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px #fff inset !important;
        -webkit-text-fill-color: #2b2a2a !important;
      }

      &:-internal-autofill-selected {
        background-color: rgba(255, 255, 255, .9) !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid #d7d7d7;
    background: transparent;
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  // min-height: 100%;
  // width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: absolute;
    width: 408px;
    max-width: 100%;
    padding: 20px 35px 45px;
    margin: 0 auto;
    overflow: hidden;
    background: #fff;
    border: 1px solid #fff;
    right: 4px;
    top: 143px;

    .el-button--primary {
      background-color: #2f90f5;
      border-color: #2f90f5;

      span {
        letter-spacing: 5px;
      }
    }

    .tt {
      position: absolute;
      border-color: #00b7fe;
      border-style: solid;
      padding: 15px;
    }

    .ul {
      border-width: 5px 0 0 5px;
      top: 0;
      left: 0;
      border-right: 5px solid transparent;
      border-bottom: 5px solid transparent;
    }

    .ur {
      border-width: 5px 5px 0 0;
      top: 0;
      right: 0;
      border-left: 5px solid transparent;
      border-bottom: 5px solid transparent;
    }

    .dl {
      border-width: 0 0 5px 5px;
      bottom: 0;
      left: 0;
      border-right: 5px solid transparent;
      border-top: 5px solid transparent;
    }

    .dr {
      border-width: 0 5px 5px 0;
      bottom: 0;
      right: 0;
      border-left: 5px solid transparent;
      border-top: 5px solid transparent;
    }

    &input::-webkit-input-placeholder {
      color: red !important;
      font-size: 20px;
    }

    &input::-moz-placeholder { /* Mozilla Firefox 19+ */
      color: red !important;
      font-size: 20px;
    }

    &input:-moz-placeholder { /* Mozilla Firefox 4 to 18 */
      color: red !important;
      font-size: 20px;
    }

    &input:-ms-input-placeholder { /* Internet Explorer 10-11 */
      color: red !important;
      font-size: 20px;
    }
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 1px 5px 3px 15px;
    color: #d7d7d7;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: #2f90f5;
      margin: 0px auto 25px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 4px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;

    .svg-icon {
      color: #d7d7d7
    }
  }
}
</style>
